package com.example.userservice.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.Ajaxresult;
import com.example.common.App;
import com.example.common.Passwordutil;
import com.example.userservice.entity.Userinfo;
import com.example.userservice.entity.UserinfoVo;
import com.example.userservice.service.Userservice;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


import static javax.swing.UIManager.put;

@RestController
@RequestMapping("/user")
public class Usercontroller {
    @Autowired
    private Userservice userservice;
    @Autowired
    private LineCaptcha lineCaptcha;
    @RequestMapping("/add")
    public Ajaxresult adduser(Userinfo userinfo){
        if(userinfo==null||!StringUtils.hasLength(userinfo.getUsername())||!StringUtils.hasLength(userinfo.getPassword())){
           return Ajaxresult.fail(-1,"参数有误");
        }
        userinfo.setPassword(Passwordutil.enctypt(userinfo.getPassword()));
       boolean result=userservice.save(userinfo);
        if(result==false){
            return Ajaxresult.fail(-1,"未知错误");
        }

        return Ajaxresult.success("注册成功",true);
    }
    @RequestMapping("/login")
    public Ajaxresult login(Userinfo userinfo,String code){
        if(!StringUtils.hasLength(userinfo.getUsername())||!StringUtils.hasLength(userinfo.getPassword())||
        !StringUtils.hasLength(code)){
            return Ajaxresult.fail(-1,"参数有误");
        }
        QueryWrapper<Userinfo>queryWrapper=new QueryWrapper<>();
        //等价于where username=#{username}
        queryWrapper.eq("username",userinfo.getUsername());
        Userinfo userinfo2=userservice.getOne(queryWrapper);
        if(userinfo2==null||userinfo2.getUid()==0){
            return Ajaxresult.fail(-1,"用户名或密码错误");
        }
        if(!Passwordutil.decypt(userinfo.getPassword(),userinfo2.getPassword())){
            return Ajaxresult.fail(-1,"用户名或密码错误");
        }
        Map<String, Object> payload = new HashMap<String, Object>() {

            private static final long serialVersionUID = 1L;
            {
                put("uid", userinfo2.getUid());
                put("manager",userinfo2.getManager());
                put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
            }
        };
        String token=JWTUtil.createToken(payload, App.JWT_KEY.getBytes());
        System.out.println(token);
        HashMap<Object,Object>data=new HashMap<>();
        data.put("token",token);
        data.put("uid",userinfo2.getUid());
        data.put("manager",userinfo2.getManager());
        data.put("username",userinfo2.getUsername());
        return Ajaxresult.success(data);

    }
    @RequestMapping("/getbyuid")
    public Ajaxresult getbyuid(Long uid, @RequestHeader(value = App.token_key,required = false)String token){
        if(uid==null||uid<=0){
            return Ajaxresult.fail(-1,"参数错误");
        }
        Userinfo userinfo=userservice.getById(uid);
        if(userinfo==null||userinfo.getUid()<=0){
            return Ajaxresult.fail(-1,"参数有误");
        }
        JWT jwt = null;
        try{
            jwt =JWTUtil.parseToken(token);
        }catch (Exception e){

        }
        String loginuid=jwt.getPayload("uid").toString();
        UserinfoVo userinfoVo=new UserinfoVo();
        userinfoVo.setUsername(userinfo.getUsername());
        userinfoVo.setEmail(userinfo.getEmail());
        userinfoVo.setLoginUid(Long.parseLong(loginuid));
        userinfoVo.setUid(userinfo.getUid());
        return Ajaxresult.success(userinfoVo);
    }
    @RequestMapping("/getusername")
    public String getusername(Long uid){
        if(uid==null||uid<=0){
            return "";
        }
        Userinfo userinfo=userservice.getById(uid);
        if(userinfo==null||userinfo.getUid()<=0){
            return "";
        }
        return userinfo.getUsername();
    }


}
